package com.iplay.dao.hotel;

import java.util.List;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.transaction.annotation.Transactional;

import com.iplay.entity.common.EntityIdDO;
import com.iplay.entity.hotel.HotelDO;

public interface HotelDAO extends PagingAndSortingRepository<HotelDO,Integer>, JpaSpecificationExecutor<HotelDO>{
	
	@Transactional
	@Modifying
	@Query("update HotelDO h set h.pictures = concat(h.pictures ,?2) where h.id = ?1")
	int addPictures(int id, String pictures);
	
	@Transactional
	@Modifying
	@Query("update HotelDO h set h.rating = ?2, h.numOfOrders = h.numOfOrders + 1 where h.id = ?1")
	int updateRatingAndIncreaseNumOfOrders(int id, double rating);
	
	@Transactional
	@Modifying
	@Query("update HotelDO h set h.rating = ?2, h.numOfReviews = h.numOfReviews+1, h.numOfOrders = h.numOfOrders + 1 where h.id = ?1")
	int updateRatingAndIncreaseNumOfReviewsAndNumOfOrders(int id, double rating);
	
	@Transactional
	@Modifying
	@Query("update HotelDO h set h.minimumPrice = ?2, h.maximumPrice = ?3 where h.id = ?1")
	int updatePriceRange(int hotelId, double minimumPrice, double maximumPrice);
	
	@Transactional
	@Modifying
	@Query("update HotelDO h set h.minimumTables = ?2, h.maximunTables = ?3 where h.id = ?1")
	int updateTableRange(int hotelId, int minimumTables, int maximumTables);
	
	List<EntityIdDO> findByName(String name);
	
	Page<HotelDO> findByNameLike(String name, Pageable pageable);
	
	Page<HotelDO> findByMinimumPriceBetween(double minimumPrice, double maximumPrice, Pageable pageable);
	
	Page<HotelDO> findByRatingGreaterThanEqual(double rating, Pageable pageable);
	
	Page<HotelDO> findByRatingGreaterThanEqualAndMinimumPriceBetween(double rating, double minimumPrice, double maximumPrice, Pageable pageable);
}
